import {
  Body,
  Controller,
  Get,
  Param,
  Post,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ArticleService } from './article.service';
import { CreateArticleDto } from './dto/create-article.dto';
import { AuthGuard } from 'src/gurad/auth/auth.guard';
import { getUserInfo } from 'src/utils/custom-decorators';

@UseGuards(AuthGuard)
@Controller('article')
export class ArticleController {
  constructor(private readonly articleService: ArticleService) {}

  /**
   * 创建文章
   */
  @Post('create')
  createArticle(
    @Body() createArticelDto: CreateArticleDto,
    @getUserInfo('id') id: number,
    @getUserInfo('role') role: string
  ) {
    return this.articleService.createArticle(createArticelDto, id, role);
  }

  /**
   * 获取文章列表
   */
  @Get('query')
  queryArticel(
    @Query() queryInfo,
    @getUserInfo('id') id: number,
    @getUserInfo('role') role: string
  ) {
    return this.articleService.queryArticel({
      ...queryInfo,
      currenPage: +queryInfo.currenPage,
      pageSize: +queryInfo.pageSize,
    }, +id, role);
  }

  /**
   * 删除文章
   */
  @Post('delete')
  delete(@Body() queryInfo) {
    return this.articleService.delete(queryInfo.id);
  }

  /**
   * 更新文章
   */
  @Post('update')
  update(@Body() upadteInfo) {
    return this.articleService.update(upadteInfo);
  }

  /**
   * 发布文章
  */
 @Post('publish')
 publish(@Body() info) {
  return this.articleService.publish(info)
 }
  /**
   * 根据文章id查询文章详情
   */
  @Get('queryArticleDetails')
  queryArticleDetails(@Query() queryInfo) {
    return this.articleService.queryDetails(+queryInfo.id);
  }

  /**
   * 文章批量删除
  */
 @Post('batchDelete')
  batchDelete(@Body() req) {
    return  this.articleService.batchDelete(req.ids)
  }

  /**
   * 获取归档文章
  */
  @Get('grouped')
  async getGroupedPosts() {
    return await this.articleService.findAllGroupedByYearMonth();
  }
}
